package com.grgbanking.it.common.json;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * Date日期类型的Json值处理器
 * @author pgwei
 *
 */
public class DateJsonValueProcessor implements JsonValueProcessor{
	private String format = "yyyy-MM-dd'T'HH:mm:ss";   
    private SimpleDateFormat sdf = new SimpleDateFormat(format);   
 
    //处理数组的值
    @Override
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return this.process(value);
    }
 
    //处理对象的值
    @Override
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        return this.process(value);
    }
     
    //处理Date类型返回的Json数值
    private Object process(Object value) {
		if (value == null) {
			return "";
		} else if (value instanceof Timestamp) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return format.format(value);
		} else if (value instanceof Date) {
			return sdf.format((Date) value);
		} else {
			return value.toString();
		}
    }
}
